package com.jnshu.service.impl;

import com.jnshu.dao.UserMapper;
import com.jnshu.dto.person.UserDto;
import com.jnshu.dto.person.WeChatAccessToken;
import com.jnshu.dto.person.WeChatUserDto;
import com.jnshu.pojo.User;
import com.jnshu.service.WeChatLoginService;
import com.jnshu.tools.wechat.WeChatUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Martin
 * @date 2019/8/6 16:46
 * 前台微信登录服务实现
 */
@Slf4j
@Service
public class WeChatLoginServiceImpl implements WeChatLoginService {
    @Autowired(required = false)
    UserMapper userMapper;

    @Override
    public UserDto getWeChatCode(String code) {
        //调用封装的微信方法，通过code值得到WeChatUserDto，网页授权的access_token返回的数据
        WeChatAccessToken weChatAccessToken = WeChatUtil.getCodeAccessToken(code);
        log.info("person服务根据controller发送的code获取到WeChatAccessToken对象为："+weChatAccessToken);
        String accessToken = weChatAccessToken.getAccessToken();
        log.info("从weChatAccessToken对象获取的accessToken为："+accessToken);
        String openId = weChatAccessToken.getOpenId();
        log.info("从weChatAccessToken对象获取的openId为："+openId);
        User user = userMapper.selectByOpenId(openId);
        log.info("根据openid在数据库中查找出来的用户信息："+user);
        //微信授权获取用户信息
        WeChatUserDto weChatUserDto = WeChatUtil.getWeChatUserInfo(accessToken,openId);
        log.info("person服务根据accessToken和openid获取到的微信用户信息为："+weChatUserDto);
        //如果数据库中有openId的用户，直接将用户信息返回给前端
        UserDto userDto = new UserDto();
        if (user != null){
            log.info("数据库中已存在openId用户，直接返回用户数据");
            userDto.setId(user.getId());
            userDto.setPhone(user.getPhone());
            userDto.setOpenId(user.getOpenId());
            userDto.setStatus(user.getStatus());
        }else {
            //若果数据库中不存在openId用户，就创建一个对象，存储在数据库中，再将授权的微信返回给前端展示
            log.info("数据库中不存在openid用户，新建一个user用户数据");
            //新建一个user
            User newUser = new User();
            //设置用户的openId
            newUser.setOpenId(weChatUserDto.getOpenid());
            //用户性别
            newUser.setSex(weChatUserDto.getSex());
            //用户昵称
            newUser.setNickname(weChatUserDto.getNickname());
            //用户头像
            newUser.setImg(weChatUserDto.getHeadImgUrl());
            //创建人
            newUser.setCreateBy(weChatUserDto.getId());
            //创建时间及更新时间
            newUser.setCreateAt(System.currentTimeMillis());
            newUser.setUpdateAt(System.currentTimeMillis());
            //用户积分
            newUser.setIntegral(0);
            //将用户状态设置为非冻结状态
            newUser.setStatus(0);
            try {
                //返回用户新增后的id
                long id = userMapper.insertSelective(newUser);
                log.info("系统生成的用户信息为：" + newUser);
                userDto.setId(id);
                userDto.setOpenId(newUser.getOpenId());
                userDto.setStatus(newUser.getStatus());
                user.setPhone(newUser.getPhone());
                log.info("返回给前端的用户信息：" + userDto);
            }catch (Exception e){
                log.error("插入数据表时捕捉到的异常为："+e);
                e.printStackTrace();
            }
        }
        return userDto;
    }
}
